package com.cyt.reward.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.ErrorCode;
import com.cyt.reward.dao.RewardShopDao;
import com.cyt.reward.model.po.RewardShop;
import com.cyt.reward.model.vo.RewardShopVo;
import com.cyt.reward.service.RewardShopService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 打赏功能-商铺管理Service层实现类
 *
 * @author rong yuan
 * @date 2019-7-24 14:13:13
 */
@Service("rewardShopService")
public class RewardShopServiceImpl extends BaseServiceImpl<RewardShop> implements RewardShopService {

    @Resource
    private RewardShopDao rewardShopDao;

    @Autowired
    public void setBaseDao(RewardShopDao rewardShopDao) {
        this.baseDao = rewardShopDao;
    }

    /**
     * 分页查询
     *
     * @param rewardShopVo 接受参数对象
     * @param pageNum      分页数
     * @param pageSize     每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-24 14:13:13
     */
    @Override
    public ReturnResult page(RewardShopVo rewardShopVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<RewardShopVo> list = rewardShopDao.selectByCondition(rewardShopVo);
        PageInfo<RewardShopVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param rewardShopVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-24 14:13:13
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(RewardShopVo rewardShopVo, SysUser sysUser) {
        if (StrUtil.isEmpty(rewardShopVo.getId())) {
            RewardShop rewardShop = new RewardShop(sysUser);
            rewardShop.setApplyId(rewardShopVo.getApplyId());
            rewardShop.setName(rewardShopVo.getName());
            rewardShop.setPassword(rewardShopVo.getPassword());
            rewardShop.setLimitDescription(rewardShopVo.getLimitDescription());
            int num = rewardShopDao.insert(rewardShop);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            RewardShop rewardShop = rewardShopDao.selectByPrimaryKey(rewardShopVo.getId());
            rewardShop.setApplyId(rewardShopVo.getApplyId());
            rewardShop.setName(rewardShopVo.getName());
            rewardShop.setPassword(rewardShopVo.getPassword());
            rewardShop.setLimitDescription(rewardShopVo.getLimitDescription());
            rewardShop.setUpdateTime(new Date());
            rewardShop.setUpdateUser(sysUser.getId());
            int num = rewardShopDao.updateByPrimaryKey(rewardShop);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param rewardShopVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2019-7-24 14:13:13
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(RewardShopVo rewardShopVo, SysUser sysUser) {
        //要修改的字段
        RewardShop rewardShop = new RewardShop();
        rewardShop.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        rewardShop.setUpdateUser(sysUser.getId());
        rewardShop.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(RewardShop.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(rewardShopVo.getId())) {
            criteria.andIn("id", Arrays.asList(rewardShopVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        rewardShopDao.updateByExampleSelective(rewardShop, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * list查询
     *
     * @param rewardShopVo 参数接受对象
     * @return List<RewardShop>
     * @author rong yuan
     * @date 2019/7/24 15:00
     */
    @Override
    public List<RewardShop> list(RewardShopVo rewardShopVo) {
        Example example = new Example(RewardShop.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("applyId", rewardShopVo.getApplyId());
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        return rewardShopDao.selectByExample(example);
    }
}